package com.itboye.jinpuyiyuan.utils.uploadmultipleimage;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.itboye.jinpuyiyuan.app.MyApplication;
import com.itboye.jinpuyiyuan.bean.UploadImageBean;
import com.itboye.jinpuyiyuan.utils.sputil.Const;


import java.io.File;
import java.util.ArrayList;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/19.
 */

public class UploadImageUtils {
    String uploadURl = Const.updaloadURL;

    public void beginUpload(Map<String, String> hashMap, String filePartName, File file, final UploadResult uploadResult) {
        MultipartRequest multipartRequest = new MultipartRequest(uploadURl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println(" onResponse    ------>>>>" + response);
                Gson gson = new Gson();
                UploadImageBean uploadImageBean=null;
                try {
                    uploadImageBean = gson.fromJson(response, UploadImageBean.class);
                    if (uploadImageBean != null) {
                        if (uploadImageBean.getCode() != 0) {
                            uploadResult.uploadFail(new VolleyError(uploadImageBean.getData().toString()));
                        } else {
                            uploadResult.uploadSuccess(uploadImageBean);
                        }
                    } else {
                        uploadResult.uploadFail(new VolleyError());
                    }
                }catch (Exception e){
                    uploadResult.uploadFail(new VolleyError(response));
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(final VolleyError error) {
                uploadResult.uploadFail(error);
            }
        }, filePartName, file, hashMap);
        multipartRequest.setRetryPolicy(new DefaultRetryPolicy(100000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        MyApplication.addRequest(multipartRequest);
    }

    public void beginUpload(Map<String, String> hashMap, String filePartName, ArrayList<File> file, final UploadResult uploadResult) {
        MultipartRequest multipartRequest = new MultipartRequest(uploadURl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                UploadImageBean uploadImageBean = gson.fromJson(response, UploadImageBean.class);
                if (uploadImageBean != null) {
                    if (uploadImageBean.getCode() != 0) {
                        uploadResult.uploadFail(new VolleyError(uploadImageBean.getData().toString()));
                    } else {
                        uploadResult.uploadSuccess(uploadImageBean);
                    }
                } else {
                    uploadResult.uploadFail(new VolleyError());
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(final VolleyError error) {
                uploadResult.uploadFail(error);
            }
        }, filePartName, file, hashMap);

        MyApplication.addRequest(multipartRequest);
    }

    public interface UploadResult {
        void uploadSuccess(UploadImageBean response);

        void uploadFail(VolleyError error);
    }
}
